//
//  YSAddressCell.m
//  Yueshi
//
//  Created by Northwan on 15/7/29.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSAddressCell.h"
#import "YSNewAddressCtrl.h"
#import "YSMineDataTool.h"
#import "YSLocationDao.h"

@interface YSAddressCell()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;

@property (weak, nonatomic) IBOutlet UILabel *addressLabel;

@end
@implementation YSAddressCell

+(instancetype)initWithTableView:(UITableView *)tableView{
    static NSString *CellIdentiferId = @"YSAddressCell";
    YSAddressCell *cell =  [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
    if(cell == nil){
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"YSAddressCell" owner:nil options:nil];
        cell = [nibs lastObject];
    }
    return cell;
}

- (IBAction)tapEditAddress:(id)sender {
    [self.viewController.navigationController pushViewController:[[YSNewAddressCtrl alloc] initWithAddress:_address] animated:YES];
}

- (IBAction)tapDelAddress:(id)sender {
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"删除地址" message:@"确定要删除吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [YSMineDataTool deleteAddressWithID:_address.addressId success:^(BaseResponse *result) {
            if(result.isSucc){
                [[NSNotificationCenter defaultCenter]postNotificationName:kAddressItemChangeNortif object:nil];
            }else{
                ShowEnterTips(result.data);
            }
        } failure:^(NSError *error) {
            ShowTips(kRequestFailure);
        }];
    }
}

-(void)setAddress:(YSAddress *)address{
    _address = address;
    [self.nameLabel setText:address.addressee];
    [self.phoneLabel setText:address.phoneCode];
    
    NSString *detailAddress = [NSString stringWithFormat:@"%@%@",address.addressStr,address.detailAdres];
    [self.addressLabel setText:detailAddress];
    [self.isDefaultAddBtn setSelected:address.defaultAdres];
}

@end
